<?php
// 无限分类2
$rows=array(
    array(
        'id'=>'1',
        'name'=>'php',
        'pid'=>'0',
        'path'=>'0'
    ),
    array(
        'id'=>'2',
        'name'=>'linux',
        'pid'=>'0',
        'path'=>'0'
    ),
    array(
        'id'=>'3',
        'name'=>'系统安装',
        'pid'=>'2',
        'path'=>'0-2'
    ),
    array(
        'id'=>'4',
        'name'=>'文件上传',
        'pid'=>'1',
        'path'=>'0-1'
    ),
    array(
        'id'=>'5',
        'name'=>'硬盘分析',
        'pid'=>'3',
        'path'=>'0-2-3'
    ),
    array(
        'id'=>'6',
        'name'=>'软件安装',
        'pid'=>'3',
        'path'=>'0-2-3'
    ),
    array(
        'id'=>'7',
        'name'=>'java',
        'pid'=>'0',
        'path'=>'0'
    )

);

// 递归遍历
function findchild(&$arr,$id){
    $childs=array();
    foreach ($arr as $k=>$v){
        if($v['pid']==$id){
            $childs[]=$v;
        }
    }
    return $childs;
}

function build_tree($root_id){
    global $rows;
    $childs=findchild($rows,$root_id);
    if(empty($childs)){ //判断空
        return null;
    }
    foreach($childs as $k=>$v){
        $rescurTree=build_tree($v['id']);
        if(null!=$rescurTree){
            $childs[$k]['childs']=$rescurTree;
        }
    }
    return $childs;
}

$trees=build_tree(0);

function show($trees){
    $str='';
    foreach($trees as $tree){
        if($tree['pid']>0){
            $pre='|'.str_repeat('-',substr_count($tree['path'],'-'));
        }else{
            $pre='';
        }
        $str.=$pre.$tree['name'].'<br>';
        if(isset($tree['childs'])){
            $str.=show($tree['childs']);
        }
    }
    return $str;
}
echo show($trees);



// echo "<pre>";
// print_r($trees);
// echo "</pre>";


?>